<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script setup>
import {onMounted} from "vue";
import {useAuthStore} from "@/stores/auth.js";
import user from "@/api/user.js";
import router from "@/router/index.js";
const authStore = useAuthStore();
onMounted(() => {
  const token = authStore.token;
  if (token) {
    const dto = {
      username: authStore.username,
      token: authStore.token
    };
    user.verifyToken(dto).then((res) => {
      if (res.data.code !== 200) {
        authStore.clearAuth();
        if (router.currentRoute.value.path !== '/login') {
          router.push("/login"); // Token无效，跳转到登录页面
        }
      } else {
        // token 有效，如果当前是登录页，则跳转到主页
        if (router.currentRoute.value.path === '/login') {
          router.push("/index/mainIndex");
        }
      }
    }).catch(() => {
      authStore.clearAuth();
      if (router.currentRoute.value.path !== '/login') {
        router.push("/login"); // 发生错误时，跳转到登录页面
      }
    });
  } else {
    if (router.currentRoute.value.path !== '/login') {
      router.push("/login"); // 没有token，跳转到登录页面
    }
  }
});
</script>

<style>
html,
body {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  background: #edf7f6;
}
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  /*widows: 100%;*/
  height: 100%;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  /* background-color:#04c1a5; */
  background-color: green;
  color: #fff;
}
.el-pagination.is-background .el-pager li:not(.disabled) {
  background-color: #04c1a5;
}
.el-select-dropdown__item:hover {
  background: #f2fff6;
}
/* 修改v-loading的默认加载样式 */
.el-loading-spinner .path{
  stroke: #42b983;
}

.el-loading-spinner .el-loading-text {
  color: #42b983;
}
.el-message-box__btns .el-button--primary {
  background-color: #42b983; /* 设置你需要的背景颜色 */
  border-color: #42b983; /* 设置你需要的边框颜色 */
  color: #fff; /* 设置你需要的文字颜色 */
}
</style>
